#include "dialog.h"
#include "ui_dialog.h"

#define DEBUG_TEST qDebug()

Dialog::Dialog(QWidget *parent) : QDialog(parent),
    ui(new Ui::Dialog){

}

Dialog::Dialog(QString dir, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    path = dir;
    //count = 3;
    ui->lineEdit->setPlaceholderText("请输入密码");
    ui->lineEdit->setEchoMode(QLineEdit::Password);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_enter_clicked()
{
    QString pw;
    pw = ui->lineEdit->text();
    QFile file(path + "//EnencryptedArea/password.txt");
    if(!file.open(QIODevice::ReadWrite | QIODevice::Text)){
        qDebug() << "Can't open the file!" << endl;
    }
//    QString displayString;
    QTextStream in(&file);
    QString line = in.readLine();
    QByteArray bytePwd = pw.toLatin1();
    QByteArray bytePwdMd5 = QCryptographicHash::hash(bytePwd, QCryptographicHash::Md5);
    QString strPwdMd5 = bytePwdMd5.toHex();
    //DEBUG_TEST << strPwdMd5;
    //DEBUG_TEST << line;
    if (strPwdMd5 == line){DEBUG_TEST << "arrive here1!";
        QString directory =
                QDir::toNativeSeparators(QFileDialog::getExistingDirectory(this,tr("选择解密存放路径"),QDir::currentPath()));
        QString srcPath = path + "/EnencryptedArea";
        directory = directory.replace("\\","/") + "/EnencryptedArea";
        qDebug()<<srcPath<< endl;
        qDebug()<<directory<< endl;
        copyRecursively(srcPath,directory);
        this->close();
    }
    else{
        count++;
        if (count==3){
            QString srcPath = path + "/EnencryptedArea";
            QString war_msg =  "密码错误已达三次！";
            QMessageBox::warning(this, "Warning", war_msg);
            QFile::copy(srcPath+"/password.txt", path+"/password.txt");
            QString s = "rm -rf "+srcPath+"/*";
            system(s.toStdString().c_str());
            QFile::copy(path+"/password.txt", srcPath+"/password.txt");
            this->close();
        }
        else
        {
            QString war_msg =  "密码错误，请重新输入！";
            QMessageBox::warning(this, "Warning", war_msg);
        }
    }
    file.close();
}

void Dialog::on_pushButton_Cencel_clicked()
{
    this->close();
}

void Dialog::on_pushButton_2_clicked()
{
    Dialog2 *MyDialog = new Dialog2(path);
    MyDialog->show();
}


bool Dialog::copyRecursively(const QString &srcFilePath,
                            const QString &tgtFilePath)
{
    QFileInfo srcFileInfo(srcFilePath);
    if (srcFileInfo.isDir()) {
        QDir targetDir(tgtFilePath);
        targetDir.cdUp();
        qDebug()<<QFileInfo(tgtFilePath).fileName()<< endl;
        if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName()))
            return false;
        QDir sourceDir(srcFilePath);
        QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
        foreach (const QString &fileName, fileNames) {
            const QString newSrcFilePath
                    = srcFilePath + QLatin1Char('/') + fileName;
            const QString newTgtFilePath
                    = tgtFilePath + QLatin1Char('/') + fileName;
            if (!copyRecursively(newSrcFilePath, newTgtFilePath))
                return false;
        }
    } else {
        if (srcFileInfo.fileName()!="password.txt")
            //QFile::copy(srcFilePath, tgtFilePath);
            MainWindow::file_decrypt(srcFilePath,tgtFilePath);
//        if (!QFile::copy(srcFilePath, tgtFilePath))
//            return false;
//        else
//        {
//            //MainWindow::file_decrypt(tgtFilePath);
//            //QFile::remove(tgtFilePath);
//        }
    }
    return true;
}
